/*
 * FilterInterface.cpp
 * Author: Michal Kolodziejski
 */

#include "FilterInterface.h"
#include "filters/RegexpFilter.h"
#include "filters/SimpleFilter.h"

FilterInterface::FilterInterface() {
	addFilter(new RegexpFilter);
	addFilter(new SimpleFilter);
}

FilterInterface::~FilterInterface() {
	reply_list.clear();
	for(unsigned int i=0;i<all_filters.size();i++)
	{
		delete all_filters.at(i);
	}
	all_filters.clear();
}

void Tokenize(const std::string& str,std::vector<std::string>& tokens,
                      const std::string& delimiters = " ")
{
    // Skip delimiters at beginning.
	std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
	std::string::size_type pos     = str.find_first_of(delimiters, lastPos);

    while (std::string::npos != pos || std::string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}



bool FilterInterface::processConfig(char* path){
	 // just testing

	    std::string config;
	    std::vector<std::string> list;
	    std::vector<std::string> templist;
	    //std::string lineEnd="\n";
	    std::string mess;

	    std::string::size_type i=0;
	    std::ifstream configFile(path);
	    //kz
	    while(std::getline(configFile,config))
	    {
	        list.push_back(config);
	    }

	    configFile.close();
	    //kz
	    reply_list.clear(); //trzeba od nowa stworzyc ta liste
	    //Tokenize(config,list,lineEnd);
	    for(i=0;i<list.size();i++)
	    {
	    	// format lini: nazwa_filtru = sciezka_do_configu_filtra

	    	if(std::string::npos!=list.at(i).find_first_of("#")) //no nic jak linia bedzie zawierac # bedzie traktowana jako komentarz
	    		continue;

	    	Tokenize(list.at(i),templist);

	    	if(templist.size()<3)
	    	{
	    		continue;
	    	}
	    	registerFilter(templist.at(0),templist.at(2));
	    	templist.clear();
	    }
	    list.clear();
	    return true;
}



bool FilterInterface::processReply(HttpStateData *httpStateData){
        //kz
    char *data=httpStateData->readBuf->content();

    std::string dataCopy(data);

    for(unsigned int i=0;i<reply_list.size();i++)
    {
        reply_list.at(i)->process(dataCopy);
    }

    httpStateData->readBuf->reset();
    httpStateData->readBuf->append(dataCopy.c_str(),dataCopy.length());
    return true;
}


FilterInterface * FilterInterface::instance = 0;


bool FilterInterface::addFilter(AbstractFilter * filter)
{
	all_filters.push_back(filter);
	return true;
}


bool FilterInterface::registerFilter(char *filterName,char *patch)
{
	std::string::size_type i=0;
	int cmp=0;
	for(i=0;i<all_filters.size();i++)
	{

		cmp=all_filters.at(i)->getName().compare(filterName);
		if(cmp==0)
		{
			bool test=false;
			test=all_filters.at(i)->processConfig(patch);
			if(test==false)
				return false;
			reply_list.push_back(all_filters.at(i));
			return true;
		}

	}
	return false;
}

bool FilterInterface::registerFilter(std::string filterName,std::string patch)
{
	std::string::size_type i=0;
	int cmp=0;
	for(i=0;i<all_filters.size();i++)
	{

		cmp=all_filters.at(i)->getName().compare(filterName);

		if(cmp==0)
		{
			bool test=false;
			test=all_filters.at(i)->processConfig(patch);
			if(test==false)
			{
				return false;
			}
			reply_list.push_back(all_filters.at(i));
			return true;
		}

	}
	return false;
}


